#!/bin/bash
set -e

# Init defaults
TYKPORT=8080
TYKTYPE="default"
TYKCONF="/opt/tyk-gateway/tyk_hybrid.conf"
REDISHOST="localhost"
REDISPORT=6379
REDISUSER=""
REDISPASS=""
REDISDB=0
REDISUSESSL=false
BINDSLUGS=true

# Parse options
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    -s|--secret)
    SECRET="$2"
    shift # past argument
    shift # past value
    ;;
    -t|--type)
    TYKTYPE="$2"
    shift # past argument
    shift # past value
    ;;
    -o|--orgid)
    ORGID="$2"
    shift # past argument
    shift # past value
    ;;
    -k|--apikey)
    APIKEY="$2"
    shift # past argument
    shift # past value
    ;;
    -p|--port)
    TYKPORT="$2"
    shift # past argument
    shift # past value
    ;;
    -c|--conf)
    TYKCONF="$2"
    shift # past argument
    shift # past value
    ;;
    --redis-host)
    REDISHOST="$2"
    shift # past argument
    shift # past value
    ;;
    --redis-port)
    REDISPORT="$2"
    shift # past argument
    shift # past value
    ;;
    --redis-user)
    REDISUSER="$2"
    shift # past argument
    shift # past value
    ;;
    --redis-pass)
    REDISPASS="$2"
    shift # past argument
    shift # past value
    ;;
    --redis-db)
    REDISDB="$2"
    shift # past argument
    shift # past value
    ;;
    --redis-use-ssl)
    REDISUSESSL=true
    shift # past argument
    ;;
    --no-bind-slugs)
    BINDSLUGS=false
    shift # past argument
    ;;
    *)
	echo "Script to setup Tyk Gateway for your Hybrid account"
	echo "Usage: setup_hybrid.sh -o <orgid> -k <apikey> -s <secret(optional, autogenerated)> -t <plugin type(optional)> -p <port(default:8080)> --redis-host <host(default:localhost)> --redis-port <port(default:6379)> --redis-user <user(optional)> --redis-pass <password(optional)> --redis-db <db(default:0)> --redis-use-ssl(optional) --no-bind-slugs(optional)"
	exit 1
	;;
esac
done

if [ -z $ORGID -o -z $APIKEY ]; then
	echo "Both organisation ID (-o) and API key (-k) are required."
	exit 1
fi

if [ -z $SECRET ]; then
	SECRET=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
	echo "Autogenerated node secret (keep it safe): $SECRET"
fi

if [ $REDISHOST == "localhost" ]; then
	echo "No Redis options detected, enabling local Redis server"
	systemctl enable redis
	systemctl start redis
fi

echo "Enabling custom configuration"
cp /opt/tyk-gateway/tyk.conf /opt/tyk-gateway/tyk.conf.bak
if [ -s $TYKCONF ]; then
	echo "Custom tyk.conf found -- using it instead of default base config"
	cp $TYKCONF /opt/tyk-gateway/tyk.conf
else
	echo "$TYKCONF file does not exist or is empty"
	exit 1
fi
echo "Writing custom Tyk Gateway env variables to /etc/default/tyk-gateway"
truncate -s 0 /etc/default/tyk-gateway
chmod 660 /etc/default/tyk-gateway
echo "TYK_GW_LISTENPORT=$TYKPORT" >> /etc/default/tyk-gateway
echo "TYK_GW_SECRET=\"$SECRET\"" >> /etc/default/tyk-gateway
echo "TYK_GW_STORAGE_HOST=$REDISHOST" >> /etc/default/tyk-gateway
echo "TYK_GW_STORAGE_PORT=$REDISPORT" >> /etc/default/tyk-gateway
echo "TYK_GW_STORAGE_USERNAME=\"$REDISUSER\"" >> /etc/default/tyk-gateway
echo "TYK_GW_STORAGE_PASSWORD=\"$REDISPASS\"" >> /etc/default/tyk-gateway
echo "TYK_GW_STORAGE_USESSL=$REDISUSESSL" >> /etc/default/tyk-gateway
echo "TYK_GW_SLAVEOPTIONS_RPCKEY=\"$ORGID\"" >> /etc/default/tyk-gateway
echo "TYK_GW_SLAVEOPTIONS_APIKEY=\"$APIKEY\"" >> /etc/default/tyk-gateway
echo "TYK_GW_SLAVEOPTIONS_BINDTOSLUGSINSTEADOFLISTENPATHS=$BINDSLUGS" >> /etc/default/tyk-gateway

# Make sure we're in the original state
systemctl stop tyk-gateway tyk-gateway-python || true
systemctl disable tyk-gateway tyk-gateway-python || true

# Launch required flavour of tyk
case $TYKTYPE in
	python)
	echo "Configuring and starting tyk-gateway-python..."
	echo "TYK_GW_COPROCESSOPTIONS_ENABLECOPROCESS=true" >> /etc/default/tyk-gateway
	systemctl enable tyk-gateway-python
	systemctl start tyk-gateway-python
	;;
	*)
	echo "Configuring and starting tyk-gateway..."
	systemctl enable tyk-gateway
	systemctl start tyk-gateway
	;;
esac

echo "Your Tyk Gateway is listening on port $TYKPORT"
echo "In case of issues please refer to the logs. You can see the logs using journalctl -u tyk-gateway."
echo "For more configuration options please check out the documentation at https://tyk.io/docs/"
